/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: Carla Painemilla
 * License Type: Evaluation
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class SubsectorDAOImpl implements orm.dao.SubsectorDAO {
	public Subsector loadSubsectorByORMID(int sub_id) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return loadSubsectorByORMID(session, sub_id);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector getSubsectorByORMID(int sub_id) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return getSubsectorByORMID(session, sub_id);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector loadSubsectorByORMID(int sub_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return loadSubsectorByORMID(session, sub_id, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector getSubsectorByORMID(int sub_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return getSubsectorByORMID(session, sub_id, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector loadSubsectorByORMID(PersistentSession session, int sub_id) throws PersistentException {
		try {
			return (Subsector) session.load(orm.Subsector.class, new Integer(sub_id));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector getSubsectorByORMID(PersistentSession session, int sub_id) throws PersistentException {
		try {
			return (Subsector) session.get(orm.Subsector.class, new Integer(sub_id));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector loadSubsectorByORMID(PersistentSession session, int sub_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Subsector) session.load(orm.Subsector.class, new Integer(sub_id), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector getSubsectorByORMID(PersistentSession session, int sub_id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Subsector) session.get(orm.Subsector.class, new Integer(sub_id), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector[] listSubsectorByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return listSubsectorByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector[] listSubsectorByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return listSubsectorByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector[] listSubsectorByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Subsector as Subsector");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Subsector[]) list.toArray(new Subsector[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector[] listSubsectorByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Subsector as Subsector");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Subsector[]) list.toArray(new Subsector[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector loadSubsectorByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return loadSubsectorByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector loadSubsectorByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return loadSubsectorByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector loadSubsectorByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Subsector[] subsectors = listSubsectorByQuery(session, condition, orderBy);
		if (subsectors != null && subsectors.length > 0)
			return subsectors[0];
		else
			return null;
	}
	
	public Subsector loadSubsectorByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Subsector[] subsectors = listSubsectorByQuery(session, condition, orderBy, lockMode);
		if (subsectors != null && subsectors.length > 0)
			return subsectors[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateSubsectorByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return iterateSubsectorByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateSubsectorByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.DiagnosticoPersistentManager.instance().getSession();
			return iterateSubsectorByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateSubsectorByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Subsector as Subsector");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateSubsectorByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Subsector as Subsector");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Subsector createSubsector() {
		return new orm.Subsector();
	}
	
	public boolean save(orm.Subsector subsector) throws PersistentException {
		try {
			orm.DiagnosticoPersistentManager.instance().saveObject(subsector);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Subsector subsector) throws PersistentException {
		try {
			orm.DiagnosticoPersistentManager.instance().deleteObject(subsector);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Subsector subsector)throws PersistentException {
		try {
			if(subsector.getCursocurso() != null) {
				subsector.getCursocurso().subsector.remove(subsector);
			}
			
			orm.Anotacion[] lAnotacions = subsector.anotacion.toArray();
			for(int i = 0; i < lAnotacions.length; i++) {
				lAnotacions[i].setSubsectorsub(null);
			}
			return delete(subsector);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Subsector subsector, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(subsector.getCursocurso() != null) {
				subsector.getCursocurso().subsector.remove(subsector);
			}
			
			orm.Anotacion[] lAnotacions = subsector.anotacion.toArray();
			for(int i = 0; i < lAnotacions.length; i++) {
				lAnotacions[i].setSubsectorsub(null);
			}
			try {
				session.delete(subsector);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Subsector subsector) throws PersistentException {
		try {
			orm.DiagnosticoPersistentManager.instance().getSession().refresh(subsector);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Subsector subsector) throws PersistentException {
		try {
			orm.DiagnosticoPersistentManager.instance().getSession().evict(subsector);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
}
